package com.neuedu.wemall.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.wemall.common.vo.ResponseData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import com.neuedu.wemall.dao.entity.Cart;
import com.neuedu.wemall.service.ICartService;

import java.util.List;

/**
 * Author lu
 * Date   2025-09-08 15:17:25
 */
@Log4j2
@RequestMapping("/wemall/member/cart/api")
@RestController
@Tag(name = "会员用户购物车控制器")
public class CartController extends BaseController {

    @Autowired
    @Qualifier("cartService")
    private ICartService cartService;

    /**
     * 添加购物车
     * @param cart  购物车实体对象
     */
    @PostMapping("/add")
    @Operation(summary = "添加购物车")
    @Parameters({
            @Parameter(name = "token", description = "令牌", required = true),
            @Parameter(name = "prodId", description = "商品ID", required = true)
    })
    public ResponseData addCart(@Parameter(hidden = false) Cart cart) {
        Integer prodId = cart.getProdId();
        if (prodId == null) {
            return ResponseData.missParam("商品ID");
        }
        int userId = getMemberUserID();
        cart.setUserId(userId);
        //添加购物车
        int rows = cartService.insertCart(cart);
        return ResponseData.success(rows);
    }

    /**
     * 删除购物车信息商品
     *
     * @param cartId
     * @return
     */
    @DeleteMapping("/remove")
    @Operation(summary = "删除购物车信息")
    @Parameters({
            @Parameter(name = "token", description = "令牌", required = true)
    })
    public ResponseData removeCart(@Parameter(name = "cartId", description = "购物车Id", required = true) @RequestParam("cartId") int cartId) {
        int userId = getMemberUserID();
        LambdaQueryWrapper<Cart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Cart::getUserId, userId);
        lqw.eq(Cart::getId, cartId);
        boolean result = cartService.remove(lqw);
        return ResponseData.success(result);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clear")
    @Operation(summary = "清空购物车")
    @Parameters({
            @Parameter(name = "token", description = "令牌", required = true)
    })
    public ResponseData clearCart() {
        int userId = getMemberUserID();
        LambdaQueryWrapper<Cart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Cart::getUserId, userId);
        boolean result = cartService.remove(lqw);
        return ResponseData.success(result);
    }

    /**
     * 修改购物车中商品数量
     *
     * @param cart 购物车实体对象
     */
    @PostMapping("/modify")
    @Operation(summary = "修改购物车中商品数量")
    @Parameters({
            @Parameter(name = "token", description = "令牌", required = true),
            @Parameter(name = "id", description = "购物车ID", required = true),
            @Parameter(name = "qty", description = "购买数量", required = true)
    })
    public ResponseData modifyCart(@Parameter(hidden = false) Cart cart) {
        int userId = getMemberUserID();
        LambdaUpdateWrapper<Cart> luw = new LambdaUpdateWrapper<>();
        luw.eq(Cart::getUserId, userId);
        luw.eq(Cart::getId, cart.getId());
        boolean result = cartService.update(cart, luw);
        return ResponseData.success(result);
    }

    /**
     * 查询会员用户购物车列表
     * @param pageNum  页码
     * @param pageSize 每页记录数
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "查询会员用户购物车列表")
    @Parameters({
            @Parameter(name = "token", description = "令牌", required = true),
            @Parameter(name = "pageSize", description = "每页记录数", required = true),
            @Parameter(name = "token", description = "令牌", required = true)

    })
    public ResponseData listCart(@RequestParam(name = "pageNum", required = true, defaultValue = "1") int pageNum,
                                 @RequestParam(name = "pageSize", required = true, defaultValue = "10") int pageSize) {
        int userId = getMemberUserID();
        Page<Cart> page = cartService.selectMemberCartList(pageNum, pageSize, userId);
        return ResponseData.success(page);
    }
}